﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyThuVien
{
    public partial class PhieuTraSach : Form
    {
        public PhieuTraSach()
        {
            InitializeComponent();
        }
      
       
        void LoadDanhSachPhieuMuon()
        {
            DataTable dt = PhieuMuonSachBUS.LayDanhSachPhieuMuon();
            combPhieuMuon.DataSource = dt;
            combPhieuMuon.DisplayMember = "MaPhieuMuonSach";
            combPhieuMuon.ValueMember = "MaPhieuMuonSach";
        }
        void LoadDanhSachPhieuTraSach()
        {
             DataTable dt = PhieuTraSachBUS.LayDanhSachPhieuTraSach();
             if (dt.Rows.Count > 0)
             {
                 dataGridViewX1.DataSource = dt;
                 dataGridViewX1.Columns["MaPhieuTraSach"].HeaderText = "Mã phiếu trả sách";
                 dataGridViewX1.Columns["MaPhieuMuonSach"].HeaderText = "Mã phiếu mượn";
                 dataGridViewX1.Columns["NgayTra"].HeaderText = "Ngày trả";
                 dataGridViewX1.Columns["TienPhat"].HeaderText = "Tiền phạt";
                 dataGridViewX1.Columns["TienNo"].HeaderText = "Tiền nợ";
                 dataGridViewX1.Columns["TongNo"].HeaderText = "Tổng nợ";
             }
        }
        void LamMoi()
        {
            foreach (Control ctrl in pnlContainer.Controls)
            {
                if (ctrl.GetType() != typeof(DevComponents.DotNetBar.ButtonX) && ctrl.GetType() != typeof(DevComponents.DotNetBar.LabelX))
                {
                    ctrl.ResetText();
                }
            }
        }
        private void PhieuTraSach_Load(object sender, EventArgs e)
        {
          
            LoadDanhSachPhieuTraSach();
            LoadDanhSachPhieuMuon();
        }

        private void btlammoi_Click(object sender, EventArgs e)
        {
            LamMoi();
        }

        private void btlapphieu_Click(object sender, EventArgs e)
        {
            PhieuTraSachDTO ptsDTO = new PhieuTraSachDTO();
            ptsDTO.Maphieumuonsach = int.Parse(combPhieuMuon.SelectedValue.ToString());
            ptsDTO.Ngaytra = dtpNgayTra.Value;
            ptsDTO.Tienno = int.Parse(txtTienNo.Text);
            ptsDTO.Tienphat = int.Parse(txtTienPhat.Text);
            ptsDTO.Tongno = ptsDTO.Tienphat + ptsDTO.Tienno;
            PhieuTraSachBUS.ThemPhieuTraSach_BUS(ptsDTO);
            LoadDanhSachPhieuTraSach();
        }

        private void btcapnhat_Click(object sender, EventArgs e)
        {
            PhieuTraSachDTO ptsDTO = new PhieuTraSachDTO();
            ptsDTO.Maphieutrasach = maPhieuTraSach;
            ptsDTO.Maphieumuonsach = int.Parse(combPhieuMuon.SelectedValue.ToString());
            ptsDTO.Ngaytra = dtpNgayTra.Value;
            ptsDTO.Tienno = int.Parse(txtTienNo.Text);
            ptsDTO.Tienphat = int.Parse(txtTienPhat.Text);
            ptsDTO.Tongno = ptsDTO.Tienphat + ptsDTO.Tienno;
            PhieuTraSachBUS.CapNhatPhieuTraSach_BUS(ptsDTO);
            LoadDanhSachPhieuTraSach();
          
        }
        int maPhieuTraSach = -1;
        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX1.SelectedRows.Count > 0 && dataGridViewX1.SelectedRows[0].IsNewRow==false)
            {
                DataGridViewRow dr = dataGridViewX1.SelectedRows[0];
                txtTienNo.Text = dr.Cells["TienNo"].Value.ToString();
                txtTienPhat.Text = dr.Cells["TienPhat"].Value.ToString();
                dtpNgayTra.Value = DateTime.Parse(dr.Cells["NgayTra"].Value.ToString());
                combPhieuMuon.SelectedValue = int.Parse(dr.Cells["MaPhieuMuonSach"].Value.ToString());
                maPhieuTraSach = int.Parse(dr.Cells["MaPhieuTraSach"].Value.ToString());
            }
        }

        private void btxoaphieu_Click(object sender, EventArgs e)
        {
            try
            {
                PhieuTraSachBUS.XoaPhieuTraSach_BUS(maPhieuTraSach);
                LoadDanhSachPhieuTraSach();
            }
            catch
            {
                MessageBox.Show("Gặp vấn đề trong quá trình xóa");
            }
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttimphieu_Click(object sender, EventArgs e)
        {
            DataTable dt = PhieuTraSachBUS.TimPhieuTraSach_BUS(dtpNgayTra.Value);
            if (dt.Rows.Count > 0)
            {
                dataGridViewX1.DataSource = dt;
                dataGridViewX1.Columns["MaPhieuTraSach"].HeaderText = "Mã phiếu trả sách";
                dataGridViewX1.Columns["MaPhieuMuonSach"].HeaderText = "Mã phiếu mượn";
                dataGridViewX1.Columns["NgayTra"].HeaderText = "Ngày trả";
                dataGridViewX1.Columns["TienPhat"].HeaderText = "Tiền phạt";
                dataGridViewX1.Columns["TienNo"].HeaderText = "Tiền nợ";
                dataGridViewX1.Columns["TongNo"].HeaderText = "Tổng nợ";
            }
            else
            {
                MessageBox.Show("Không tìm thấy dữ liệu");
            }
        }

        
       
        
      
        
        
    }
}